//以下为未登录拦截，登录注册，主页，发帖，帖子详情的主要代码
//未登录拦截，未登录就跳转到首页
/**
 * 登录拦截器
 * 
 */
public class LoginInterceptor implements HandlerInterceptor {
	@Override
	public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
			throws Exception {
		// 获取请求的URL
		String url = request.getRequestURI();
		if (url.indexOf("/index.do") >= 0 || url.indexOf("/login.do") >= 0) {
			return true;
		}
		if (url.indexOf("/register.do") >= 0 ) {
			return true;
		}
		
		HttpSession session = request.getSession();
		User user = (User) session.getAttribute("USER_SESSION");
		//未登录
		if (user != null) {
			return true;
		}

		request.setAttribute("msg", "您还未登录，请登录");

		request.getRequestDispatcher("/WEB-INF/jsp/index.jsp").forward(request, response);
		return false;
	}

	@Override
	public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler,
			ModelAndView modelAndView) throws Exception {
	}

	@Override
	public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex)
			throws Exception {
	}
}



//登录注册

	/**
	 * 用户登录
	 */
	@RequestMapping(value = "/login.do", method = RequestMethod.POST)
	public String login(String username, String password, Model model, HttpSession session) {
		User user = userService.findUser(username, password);
		if (user != null) {

			session.setAttribute("USER_SESSION", user);
			return "redirect:home.do";
		}
		model.addAttribute("msg", "账号或密码错误，请重新输入！");
		return "index";
	}

	@RequestMapping(value = "/register.do", method = RequestMethod.POST)
	public String register(Model model, String username, String password, String email, HttpSession session) {
		User user = new User();
		user.setUser_name(username);
		user.setUser_pass(password);
		user.setUser_email(email);
		session.setAttribute("USER_SESSION", user);
		userService.register(user);

		return "forward:/home.do";
	}

	/**
	 * 退出登录
	 */
	@RequestMapping(value = "/logout.do")
	public String logout(HttpSession session) {
		session.invalidate();
		return "index";
	}



//主页
@RequestMapping("/home.do")
	public String list(Model model) {
		List<Category> streetDanceCateList = categoryService.findCateList();
		List<Blogs> todayRecommendList = blogsService.findTodayrcmdBlogsList();
		List<Blogs> hotRecommendList =  blogsService.findhotrcmdblogsList();
		List<Blogs> streetmatchlist =  blogsService.findmatchList();
		model.addAttribute("streetDanceCateList", streetDanceCateList);
		model.addAttribute("todayRecommendList", todayRecommendList);
		model.addAttribute("hotRecommendList", hotRecommendList);
		model.addAttribute("streetmatchlist", streetmatchlist);
		return "home";
	}


//发帖

@RequestMapping("/postBlog.do")
	public String add(Model model, String blogs_name, String blogs_disc) {

		return "postBlog";
	}

	@RequestMapping("/postBlogView.do")
	public String addview(@RequestParam("file") MultipartFile file, Blogs blogs,Model model, String blogs_name,
			String blogs_disc, HttpServletRequest request) throws IllegalStateException, IOException {
		blogs.setBlogs_name(blogs_name);
		blogs.setBlogs_disc(blogs_disc);
		// 图片上传成功后，将图片的地址写到数据库
		String addimgname = UUID.randomUUID().toString().replaceAll("-", "");
		// 获取原始图片的拓展名
		String getimgtxt = FilenameUtils.getExtension(file.getOriginalFilename());
		// 新的文件名字
		String newimgurl = request.getSession().getServletContext().getRealPath("/upload");
		// System.out.println(url);
		// 以绝对路径保存重名命后的图片
		file.transferTo(new File(newimgurl + "/" + addimgname + "." + getimgtxt));
		// 把图片存储路径保存到数据库
		blogs.setBlogs_pic("upload/" + addimgname + "." + getimgtxt);

		/**
		 * 保存商品
		 */
		blogsService.addblog(blogs);
		return "redirect:blogdetail.do";
	}

    //帖子详情

    	@RequestMapping("/blogdetail.do")
	public String blogdetail(Model model, Integer blogs_id) {
		List<Blogs> =blogsdetaillist = blogsService.findblogsdetailbyid(blogs_id);
		List<Category> catelist2 = categoryService.findCateList();
		model.addAttribute("catelist2", catelist2);
		model.addAttribute("blogsdetaillist", blogsdetaillist);
		return "blog";
	}

